﻿#define _CRT_SECURE_NO_WARNINGS 1

#include "Contact.h"


void LoadContact(contact** head)
{
	

	FILE* pfRead = fopen("Contact.txt", "r");
	if (pfRead == NULL)
	{
		perror("LoadContact");
		exit(0);
	}
	while (fgetc(pfRead) != '\n');//将文件指针移动到表头末尾
	int leap = ftell(pfRead);//计算文件指针的偏移量
	fseek(pfRead, leap, SEEK_SET);//将文件指针后移，跳过表头，只读取数据
	while (!feof(pfRead))
	{
		contact* pnew = *head;
		contact* pmove = *head;
		pnew = (contact*)malloc(sizeof(contact));

		pnew->next = NULL;
		//将文件的内容写入节点中
		fscanf(pfRead, "%10s\t%4d\t  %6s\t%12s%20s\n", pnew->data.name,
			&(pnew->data.units),
			pnew->data.QQ,
			pnew->data.tele,
			pnew->data.addr);
		//链接节点
		if (*head == NULL)//第一次添加
		{
			*head = pnew;
			pnew->next = NULL;
		}
		else
		{
			while (pmove->next != NULL)
			{
				pmove = pmove->next;
			}
			
			pmove->next = pnew;
			pnew->next = NULL;
		}
	}
	
	printf("登录成功！\n");

	fclose(pfRead);
	pfRead = NULL;
}




//释放空间
void DestroyContact(contact** head)
{
	contact* pmove = *head;
	while (*head != NULL)
	{
		pmove = *head;//记录表头的地址
		*head = (*head)->next;//表头后移，直到最后一个节点
		free(pmove);
		pmove->next = NULL;
	}
	
}




void AddContact(contact** head)
{
	assert(head);//进行断言防止pc为空指针，它的头文件为<assert.h>

	contact* pnew = *head;
	contact* pmove = *head;

	pnew = (contact*)malloc(sizeof(contact));

	//写入信息
	printf("请输入姓名：>");
	scanf("%s", pnew->data.name);
	printf("请输入单位：>");
	scanf("%d", &(pnew->data.units));
	printf("请输入QQ：>");
	scanf("%s", pnew->data.QQ);
	printf("请输入电话：>");
	scanf("%s", pnew->data.tele);
	printf("请输入地址：>");
	scanf("%s", pnew->data.addr);
	pnew->next = NULL;
	if (*head == NULL)//第一次添加
	{
		*head = pnew;
		pnew->next = NULL;
	}
	else
	{
		while (pmove->next != NULL)//第二次及以后添加节点
		{
			pmove = pmove->next;//找到尾节点

		}
		pmove->next = pnew;
		pnew->next = NULL;
	}
}

void ShowContact(const contact** head)
{
	system("cls");
	contact* pmove = *head;
	printf("%10s\t%5s\t%10s\t%12s\t%20s\n", "姓名", "单位", "QQ", "电话", "地址");//打印一个表头在屏幕上
	while (pmove != NULL)//循环到最后一个节点停止
	{
		printf("%10s\t%5d\t%10s\t%12s\t%20s\n", pmove->data.name,
			pmove->data.units,
			pmove->data.QQ,
			pmove->data.tele,
			pmove->data.addr);
		pmove = pmove->next;
	}
}


void DelContact(contact** head)
{
	assert(head);
	if (*head == NULL)
	{
		printf("通讯录为空，删除失败！\n");
		exit(0);
	}
	char name[20];
	printf("请输入要删除联系人的姓名：>");
	scanf("%s", name);
	//删除
	contact* pmove = *head;
	contact* plast = *head;
	//找到要删除的节点，同时移动节点的时候当移动到最后一个节点还没有找到就停止移动节点防止越界
	while (strcmp(pmove->data.name, name) != 0 && pmove->next!=NULL)
	{
		plast = pmove;//保存移动节点的上一个节点
		pmove = pmove->next;
	}
	if (strcmp(pmove->data.name, name) == 0)
	{
		//如果删除的是表头节点
		if (pmove == *head)
			*head = pmove->next;
		else//如果删除的是中间的节点
			plast->next = pmove->next;
		printf("删除成功！\n");
	}
	else
		printf("无此联系人，无法删除！\n");
}

void SearchContact(const contact** head)
{
	char name[20];
	printf("请输入你要查找联系人的姓名：>");
	scanf("%s", name);
	contact* pos = *head;
	//找到要查看到节点
	while (strcmp(pos->data.name, name) != 0 && pos->next != NULL)
	{
		pos = pos->next;
	}
	if (strcmp(pos->data.name, name) == 0)
	{
		printf("%10s\t%5s\t%10s\t%12s\t%20s\n", "姓名", "单位", "QQ", "电话", "地址");//打印一个表头，要注意打印形式
		printf("%10s\t%5d\t%10s\t%12s\t%20s\n", pos->data.name,
			pos->data.units,
			pos->data.QQ,
			pos->data.tele,
			pos->data.addr);
	}
	else
		printf("无此联系人！\n");
}
void ModifyContact(contact** head)
{
	char name[20];
	printf("请输入你要修改联系人的姓名\n");
	scanf("%s", name);
	contact* pos = *head;
	//找到该联系人
	while (strcmp(pos->data.name, name) != 0 && pos->next != NULL)
	{
		pos = pos->next;
	}
	if (strcmp(pos->data.name, name) == 0)
	{
		printf("请输入姓名：>");
		scanf("%s", pos->data.name);
		printf("请输入单位：>");
		scanf("%d", &(pos->data.units));
		printf("请输入QQ：>");
		scanf("%s", pos->data.QQ);
		printf("请输入电话：>");
		scanf("%s", pos->data.tele);
		printf("请输入地址：>");
		scanf("%s", pos->data.addr);
		printf("修改成功!\n");
	}
	else
	{
		printf("未查找到该联系人,无法修改!\n");
		exit(0);
	}
}

void SortContact(contact** head)//利用姓名进行排序
{
	contact* pmove = *head;
	contact* pcurrent = *head;
	contact temp;//交换时的临时变量
	if (*head == NULL)
	{
		printf("通讯录为空，无法排序！\n");
		exit(0);
	}
	if ((*head)->next == NULL)//链表只有一个节点时不需要排序直接返回
	{
		exit(0);
	}
	while (pcurrent->next != NULL)
	{
		pmove = pcurrent;//
		while (pmove != NULL)
		{
			if (strcmp(pmove->data.name, pcurrent->data.name) < 0)
			{
				temp = *pmove;
				*pmove = *pcurrent;
				*pcurrent = temp;//交换数值
				temp.next = pmove->next;
				pmove->next = pcurrent->next;
				pcurrent->next = temp.next;//交换指针域
			}
			pmove = pmove->next;
		}
		pcurrent = pcurrent->next;
	}
	printf("排序成功！\n");
}
void SaveContact(contact** head)
{
	assert(*head);
	contact* pmove = *head;
	FILE* pfWrite = fopen("Contact.txt", "w");
	if (pfWrite == NULL)
	{
		perror("SaveContact");
		exit(0);
	}
	int i = 0;
	fprintf(pfWrite, "%-10s\t%5s\t%10s\t%12s\t %20s\n",
		"姓名", "单位", "QQ", "电话", "地址");
	while(pmove!=NULL)
	{
		fprintf(pfWrite, "%-10s\t%4d\t  %6s\t%12s%20s\n", pmove->data.name,
			pmove->data.units,
			pmove->data.QQ,
			pmove->data.tele,
			pmove->data.addr);
		pmove = pmove->next;
	}
	fclose(pfWrite);
	pfWrite = NULL;
}
